﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Manufacturers
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private ManufacturerBiz _manufacturerBiz;
        private ManufacturerBiz ManufacturerManager
        {
            get { return _manufacturerBiz ?? (_manufacturerBiz = new ManufacturerBiz()); }
        }

        private int _manufacturerId;
        private int ManufacturerId
        {
            get
            {
                if (_manufacturerId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _manufacturerId);
                return _manufacturerId;
            }
        }

        private Manufacturer _manufacturer;
        private Manufacturer CurrentManufacturer
        {
            get
            {
                if (_manufacturer == null && ManufacturerId > 0)
                {
                    _manufacturer = ManufacturerManager.GetById(ManufacturerId);
                }
                return _manufacturer;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                // load manufacturer groups
                var groups = ManufacturerManager.GetAllManufacturerGroups();//_repository.CityRepository.GetAllManufacturerGroups();
                groups.Insert(0, new ManufacturerGroup { Id = 0 });
                ddlManufacturerGroups.DataTextField = "Name";
                ddlManufacturerGroups.DataValueField = "Id";
                ddlManufacturerGroups.DataSource = groups;
                ddlManufacturerGroups.DataBind();

                // Load manufacturer data
                if (CurrentManufacturer == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", Resources.GlobalStrings.NotExisted, ManufacturerId);
                    btnSave.Enabled = false;
                }
                else
                {
                    ddlManufacturerGroups.SelectedValue = CurrentManufacturer.ManufacturerGroupId.ToString();
                    txtCode.Text = CurrentManufacturer.Code;
                    txtName.Text = CurrentManufacturer.Name;
                    txtAddress.Text = CurrentManufacturer.Address;
                    txtPhone.Text = CurrentManufacturer.Phone;
                    txtFax.Text = CurrentManufacturer.Fax;
                    txtEmail.Text = CurrentManufacturer.Email;
                    txtWebsite.Text = CurrentManufacturer.Website;
                    txtTaxCode.Text = CurrentManufacturer.TaxCode;
                    txtBankAccount.Text = CurrentManufacturer.BankAccount;
                    txtBankName.Text = CurrentManufacturer.BankName;
                    txtBankProvince.Text = CurrentManufacturer.BankProvince;
                    txtDirectorName.Text = CurrentManufacturer.DirectorName;
                    txtAccounterName.Text = CurrentManufacturer.AccounterName;
                    txtDescription.Text = CurrentManufacturer.Description;
                    txtSortOrder.Text = CurrentManufacturer.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentManufacturer.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var address = txtAddress.Text.Trim();
            var phone = txtPhone.Text.Trim();
            var fax = txtFax.Text.Trim();
            var email = txtEmail.Text.Trim();
            var website = txtWebsite.Text.Trim();
            var taxcode = txtTaxCode.Text.Trim();
            var bankAccount = txtBankAccount.Text.Trim();
            var bankName = txtBankName.Text.Trim();
            var bankProvince = txtBankProvince.Text.Trim();
            var director = txtDirectorName.Text.Trim();
            var accounter = txtAccounterName.Text.Trim();
            int manufacturerGroupId;
            int.TryParse(ddlManufacturerGroups.SelectedValue, out manufacturerGroupId);

            // get data
            CurrentManufacturer.Code = code;
            CurrentManufacturer.Name = name;
            CurrentManufacturer.Address = address;
            CurrentManufacturer.Phone = phone;
            CurrentManufacturer.Fax = fax;
            CurrentManufacturer.Email = email;
            CurrentManufacturer.Website = website;
            CurrentManufacturer.TaxCode = taxcode;
            CurrentManufacturer.BankAccount = bankAccount;
            CurrentManufacturer.BankName = bankName;
            CurrentManufacturer.BankProvince = bankProvince;
            CurrentManufacturer.DirectorName = director;
            CurrentManufacturer.AccounterName = accounter;
            CurrentManufacturer.Description = descrition;
            CurrentManufacturer.SortOrder = sortOrder;
            CurrentManufacturer.Hidden = isHidden;
            CurrentManufacturer.ManufacturerGroupId = manufacturerGroupId;

            // save data
            ManufacturerManager.UpdateManufacturer(CurrentManufacturer);

            // return
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}